home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / wildcat / wc30rec.zip / BTREEF.PAS next >
Pascal/Delphi Source File  |  1992-05-12  |  8KB  |  251 lines

  1.   function BuildFileKey(var Data : FileRecordType; KeyNr : Byte) : IsamKeyStr;
  2.    {-Build keys for file record}
  3.   begin
  4.     with Data do
  5.       case KeyNr of
  6.         FileNameKey : BuildFileKey := PackFileName(FileName);
  7.         FileAreaKey : BuildFileKey := WordToKey(Area)+PackFileName(FileName);
  8.         FileDateKey : BuildFileKey := WordToKey(Area)+WordToKey(DateAndTime.D)+LongToKey(DateAndTime.T);
  9.         FilePWKey   : BuildFileKey := Long2Str(Ord(WordFlagSet(FileToggles, fiPasswordReq)))+PackFileName(FileName);
  10.         FileUpKey   : if UploadedBy <> '' then
  11.                         BuildFileKey := Pack6BitKeyUC(UploadedBy, 19)
  12.                       else
  13.                         BuildFileKey := '';
  14.       end;
  15.   end;
  16.  
  17.  
  18.   function BuildKWKey(KeyWord : Str10) : Str08;
  19.    {-Build keyword key}
  20.   begin
  21.     if KeyWord = '' then
  22.       BuildKWKey := ''
  23.     else
  24.       BuildKWKey := Pack6BitKeyUC(KeyWord, 8);
  25.   end;
  26.  
  27.  
  28.   procedure CleanUpKeyWords(var KeyWords : KeyArray);
  29.     {-Removes duplicate keywords}
  30.   var
  31.     X, Y  : Byte;
  32.     SearchName : SearchArray;
  33.  
  34.     procedure RemoveIfDup(StartPos : Byte);
  35.     var
  36.       I              : Byte;
  37.       Key            : String[8];
  38.  
  39.     begin
  40.       Key := Pack6BitKeyUC(SearchName[StartPos], 8);
  41.       for I := StartPos + 1 to 6 do
  42.         if (Key = Pack6BitKeyUC(SearchName[I], 8)) then
  43.           begin
  44.             SearchName[StartPos] := '';
  45.             Exit;
  46.           end;
  47.     end;
  48.  
  49.   begin
  50.     for Y := 1 to 6 do
  51.       SearchName[Y] := stupcase(Trim(KeyWords[Y]));
  52.  
  53.     for Y := 1 to 5 do
  54.       if (SearchName[Y] <> '') then
  55.         RemoveIfDup(Y);
  56.  
  57.     if (KeyStringFound('BADKEYS.LST', SearchName, False)) then
  58.       {ignore};
  59.  
  60.     X := 0;
  61.     for Y := 1 to 6 do
  62.       if (SearchName[Y] <> '') then
  63.         begin
  64.           Inc(X);
  65.           KeyWords[X] := SearchName[Y];
  66.         end;
  67.  
  68.     for Y := X + 1 to 6 do
  69.       KeyWords[Y] := '';
  70.   end;
  71.  
  72.  
  73.   function AddFileRecord(var Data : FileRecordType) : Boolean;
  74.     {-Add a new file record to the database}
  75.   var
  76.     X, KeyNr : Byte;
  77.     RefNr : LongInt;
  78.     Key : IsamKeyStr;
  79.     LockStatus : Boolean;
  80.     Len : Word;
  81.  
  82.   begin
  83.     AddFileRecord := False;
  84.     LockStatus := LockBTree(dbFile);
  85.     FindBtreeKey(FileSpec, RefNr, BuildFileKey(Data, FileNameKey), FileNameKey);
  86.     if not IsamOk then
  87.       with Data do
  88.         begin
  89.           FileName := StUpcase(FileName);
  90.           CleanUpKeyWords(KeyWords);
  91.           Len := (SizeOf(FileRecordType) - 2048) + TotalMsgBytes;
  92.           BtAddVariableRec(FileSpec, RefNr, Data, Len);
  93.           if not IsamOk then
  94.             LogFatalError('Unable to add file record', IsamError);
  95.           for KeyNr := 1 to KeysDatabase[dbFile] do
  96.             if KeyNr < 6 then
  97.               begin
  98.                 Key := BuildFileKey(Data, KeyNr);
  99.                 if Key <> '' then
  100.                   begin
  101.                     BtAddKey(FileSpec, KeyNr, RefNr, Key);
  102.                     if not IsamOk then
  103.                       LogFatalError('Unable to add file key #'+Long2Str(KeyNr), IsamError);
  104.                   end;
  105.               end
  106.             else
  107.               for X := 1 to 6 do
  108.                 if KeyWords[X] <> '' then
  109.                   begin
  110.                     Key := BuildKwKey(KeyWords[X]);
  111.                     BtAddKey(FileSpec, KeyNr, RefNr, Key);
  112.                     if not IsamOk then
  113.                       LogFatalError('Unable to add file key #'+Long2Str(KeyNr), IsamError);
  114.                   end;
  115.           AddFileRecord := True;
  116.         end;
  117.     if LockStatus then
  118.       UnLockBtree(dbFile);
  119.   end;
  120.  
  121.  
  122.   function UpdateFileRecord(var NewData : FileRecordType; OldName : Str12) : Boolean;
  123.     {-Update an existing file record}
  124.   label
  125.     ExitPoint;
  126.  
  127.   var
  128.     X,
  129.     KeyNr          : Byte;
  130.     OldData        : FileRecordType;
  131.     RefNr          : LongInt;
  132.     Key            : IsamKeyStr;
  133.     LockStatus     : Boolean;
  134.  
  135.   begin
  136.     UpdateFileRecord := False;
  137.     LockStatus := LockBTree(dbFile);
  138.     OldName := StUpcase(OldName);
  139.     NewData.FileName := StUpcase(NewData.FileName);
  140.     if OldName <> NewData.FileName then
  141.       if FileInDataBase(NewData.FileName, RefNr) then
  142.         goto ExitPoint;
  143.     if FileInDataBase(OldName, RefNr) then
  144.       begin
  145.         CleanUpKeyWords(NewData.KeyWords);
  146.         GetBtreeVarRec(FileSpec, RefNr, OldData);
  147.         if not IsamOk then
  148.           LogFatalError('Unable to load file record', IsamError);
  149.         for KeyNr := 1 to KeysDatabase[dbFile] do
  150.           if KeyNr < 6 then
  151.             begin
  152.               Key := BuildFileKey(OldData, KeyNr);
  153.               if (Key <> '') and (Key <> BuildFileKey(NewData, KeyNr)) then
  154.                 begin
  155.                   BtDeleteKey(FileSpec, KeyNr, RefNr, Key);
  156.                   if not IsamOk then
  157.                     LogFatalError('Unable to delete file key #'+Long2Str(KeyNr), IsamError);
  158.                 end;
  159.             end
  160.           else
  161.             for X := 1 to 6 do
  162.               begin
  163.                 Key := BuildKWKey(OldData.KeyWords[X]);
  164.                 if (Key <> '') and (Key <> BuildKWKey(NewData.KeyWords[X])) then
  165.                   begin
  166.                     BtDeleteKey(FileSpec, KeyNr, RefNr, Key);
  167.                     if not IsamOk then
  168.                       LogFatalError('Unable to delete file key #'+Long2Str(KeyNr), IsamError);
  169.                   end;
  170.               end;
  171.         BtPutVariableRec(FileSpec, RefNr, NewData, (SizeOf(FileRecordType) - 2048) + NewData.TotalMsgBytes);
  172.         if not IsamOk then
  173.           LogFatalError('Unable to update file record', IsamError);
  174.         for KeyNr := 1 to KeysDatabase[dbFile] do
  175.           if KeyNr < 6 then
  176.             begin
  177.               Key := BuildFileKey(NewData, KeyNr);
  178.               if (Key <> '') and (Key <> BuildFileKey(OldData, KeyNr)) then
  179.                 begin
  180.                   BTAddKey(FileSpec, KeyNr, RefNr, Key);
  181.                   if not IsamOk then
  182.                     LogFatalError('Unable to add file key #'+Long2Str(KeyNr), IsamError);
  183.                 end;
  184.             end
  185.           else
  186.             for X := 1 to 6 do
  187.               begin
  188.                 Key := BuildKWKey(NewData.KeyWords[X]);
  189.                 if (Key <> '') and (Key <> BuildKWKey(OldData.KeyWords[X])) then
  190.                   begin
  191.                     BTAddKey(FileSpec, KeyNr, RefNr, Key);
  192.                     if not IsamOk then
  193.                       LogFatalError('Unable to add file key #'+Long2Str(KeyNr), IsamError);
  194.                   end;
  195.               end;
  196.         UpdateFileRecord := True;
  197.       end;
  198.   ExitPoint:
  199.     if LockStatus then
  200.       UnLockBtree(dbFile);
  201.   end;
  202.  
  203.  
  204.   function DeleteFileRecord(var Data : FileRecordType) : Boolean;
  205.     {-Delete file record from database}
  206.   var
  207.     X, KeyNr : Byte;
  208.     RefNr : LongInt;
  209.     Key : IsamKeyStr;
  210.     LockStatus     : Boolean;
  211.  
  212.   begin
  213.     DeleteFileRecord := False;
  214.     LockStatus := LockBTree(dbFile);
  215.     FindBtreeKey(FileSpec, RefNr, BuildFileKey(Data, FileNameKey), FileNameKey);
  216.     if IsamOk then
  217.       begin
  218.         GetBtreeVarRec(FileSpec, RefNr, Data);
  219.         if not IsamOk then
  220.           LogFatalError('Unable to load file record', IsamError);
  221.         for KeyNr := 1 to KeysDatabase[dbFile] do
  222.           if KeyNr < 6 then
  223.             begin
  224.               Key := BuildFileKey(Data, KeyNr);
  225.               if Key <> '' then
  226.                 begin
  227.                   BtDeleteKey(FileSpec, KeyNr, RefNr, Key);
  228.                   if not IsamOk then
  229.                     LogFatalError('Unable to delete file key #'+Long2Str(KeyNr), IsamError);
  230.                 end;
  231.             end
  232.           else
  233.             for X := 1 to 6 do
  234.               begin
  235.                 Key := BuildKWKey(Data.KeyWords[X]);
  236.                 if Key <> '' then
  237.                   begin
  238.                     BtDeleteKey(FileSpec, KeyNr, RefNr, Key);
  239.                     if not IsamOk then
  240.                       LogFatalError('Unable to delete file key #'+Long2Str(KeyNr), IsamError);
  241.                   end;
  242.               end;
  243.         BtDeleteVariableRec(FileSpec, RefNr);
  244.         if not IsamOk then
  245.           LogFatalError('Unable to delete file record', IsamError);
  246.         DeleteFileRecord := True;
  247.       end;
  248.     if LockStatus then
  249.       UnLockBtree(dbFile);
  250.   end;
  251.